/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Services;

import com.ControlEscolar.Beans.Group;
import com.ControlEscolar.Beans.GroupDetail;
import com.ControlEscolar.DAOs.DAOException;
import com.ControlEscolar.DAOs.ExamDAO;
import com.ControlEscolar.DAOs.GroupDAO;
import com.ControlEscolar.DAOs.GroupDetailDAO;
import com.ControlEscolar.Exceptions.AlreadyExistExp;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Validations.ValidationExam;
import com.ControlEscolar.Validations.ValidationGroup;
import java.util.ArrayList;




/**
 *
 * @author Bere
 */
public class GroupService {
     GroupDAO groupDAO=new GroupDAO();
     GroupDetailDAO groupDetailDAO=new GroupDetailDAO();
    ValidationGroup validationGroup=new ValidationGroup();
    ValidationExam validationExam =new ValidationExam();
    ReticleService reticleService=new ReticleService();
    ExamDAO examDAO=new ExamDAO();

    public void createGroup(Group group) throws  AlreadyExistExp, ServiceExp{
        try{
        if(validationGroup.isExistGroup(group.getGroupName())){
            throw new AlreadyExistExp("Ya existe Grupo");
        }
        else
            groupDAO.add(group);}
        catch(DAOException e){
            throw new ServiceExp();
        }
    }

    public void updateState(Group group) throws ServiceExp {
       try{
        groupDAO.update(group);
       }
       catch(DAOException e){
            throw new ServiceExp();
        }

    }

    public Group getGroup(String groupName) throws ServiceExp{
        try {
            return groupDAO.queryByName(groupName);
        } catch (DAOException ex) {
           throw new ServiceExp();
        }
    }

    public ArrayList<Group> getGroupList() throws ServiceExp{
        try {
            return groupDAO.query();
        } catch (DAOException ex) {
            throw new ServiceExp();
        }
    }

    public GroupDetail getGroupDetail(String groupName) throws ServiceExp{
         try {
            return groupDetailDAO.queryByName(groupName);
        } catch (DAOException ex) {
           throw new ServiceExp();
        }
    }

    public ArrayList<GroupDetail> getListGroupsDetail() throws ServiceExp{
        try {
            return groupDetailDAO.query();
        } catch (DAOException ex) {
            throw new ServiceExp();
        }
    }

    public ArrayList <Group> getGroupsToStudent(int ID_Curriculum, int semester,String studentKey ) throws ServiceExp{
        try {
            ArrayList<String> allSubjects = reticleService.getSubjectsToStudent(ID_Curriculum, semester);
            ArrayList subjectsList = subjectWithoutGradeOrFailed(allSubjects, studentKey);
            return groupDAO.queryStudentGroupsAvailable(subjectsList);
        } catch (DAOException ex) {
            throw new ServiceExp();
        }
    }

     protected ArrayList <GroupDetail> getGroupsDetailToStudent(int ID_Curriculum, int semester,String studentKey ) throws DAOException {
          ArrayList <String> allSubjects=reticleService.getSubjectsToStudent(ID_Curriculum, semester);
          ArrayList subjectsList=subjectWithoutGradeOrFailed(allSubjects,studentKey);
          return groupDetailDAO.queryStudentGroupsDetailedAvailable(subjectsList);
    }

     private ArrayList<String> subjectWithoutGradeOrFailed(ArrayList <String> allSubjects,String studentKey) throws DAOException{
        ArrayList<String> subjectsList=new ArrayList();
        for(int index=0;index<allSubjects.size();index++){
            if(validationExam.examWithoutGradeOrFailed(allSubjects.get(index), studentKey)){
                subjectsList.add(allSubjects.get(index));
            }
        }

      return subjectsList;
    }

    





}
